<!--
  * 租户 员工
-->
<template>
  <div>
    <div class="header">
      <div>
        用户名：
        <a-input style="width: 250px" v-model:value="queryForm.userName" placeholder="姓名" />
        手机号：
        <a-input style="width: 250px" v-model:value="queryForm.phoneNumber" placeholder="手机号" />
        登录账号：
        <a-input style="width: 250px" v-model:value="queryForm.account" placeholder="登录账号" />
        <a-button class="button-style" type="primary" @click="queryEmployee">搜索</a-button>
        <a-button class="button-style" type="default" @click="resetQueryEmployee">重置</a-button>
      </div>
    </div>
    <a-table
      :loading="tableLoading"
      :dataSource="tableData"
      :columns="columns"
      :pagination="false"
      rowKey="id"
      size="small"
      bordered
    >
      <template #bodyCell="{ text, record, index, column }">
        <template v-if="column.dataIndex === 'state'">
          <span>{{ $smartEnumPlugin.getDescByValue('FLAG_ENUM', text) }}</span>
        </template>
        <template v-else-if="column.dataIndex === 'gender'">
          <span>{{ $smartEnumPlugin.getDescByValue('GENDER_ENUM', text) }}</span>
        </template>
      </template>
    </a-table>
    <div class="smart-query-table-page">
      <a-pagination
        showSizeChanger
        showQuickJumper
        show-less-items
        :pageSizeOptions="PAGE_SIZE_OPTIONS"
        :defaultPageSize="queryForm.pageSize"
        v-model:current="queryForm.pageIndex"
        v-model:pageSize="queryForm.pageSize"
        :total="total"
        @change="queryEmployee"
        @showSizeChange="queryEmployee"
        :show-total="showTableTotal"
      />
    </div>
  </div>
</template>
<script setup lang="ts">
  import { message, Modal } from 'ant-design-vue';
  import _ from 'lodash';
  import { reactive, ref, watch } from 'vue';
  import { enterpriseApi } from '/@/api/business/oa/enterprise-api';
  import { PAGE_SIZE, PAGE_SIZE_OPTIONS, showTableTotal } from '/@/constants/common-const';
  import { smartSentry } from '/@/lib/smart-sentry';

  const props = defineProps({
    enterpriseId: {
      type: String,
      default: null,
    },
  });

  const columns = reactive([
    {
      title: '姓名',
      dataIndex: 'userName',
      width: 150,
    },
    {
      title: '手机号',
      dataIndex: 'phoneNumber',
      width: 120,
    },
    {
      title: '登录账号',
      dataIndex: 'account',
      width: 150,
    },
    {
      title: '部门',
      dataIndex: 'orgName',
      width: 200,
      ellipsis: true,
    },
    {
      title: '状态',
      dataIndex: 'state',
      width: 80,
    },
  ]);

  // --------------------------- 查询 ---------------------------

  const defaultQueryForm = {
    pageIndex: 1,
    pageSize: PAGE_SIZE,
    tenantId: undefined,
    userName: undefined,
    account: undefined,
    phoneNumber: undefined,
  };
  // 查询表单
  const queryForm = reactive({ ...defaultQueryForm });
  const total = ref(0);
  const tableData = ref([]);
  const tableLoading = ref(false);

  function resetQueryEmployee() {
    queryForm.userName = '';
    queryForm.account = '';
    queryForm.phoneNumber = '';
    queryEmployee();
  }

  async function queryEmployee() {
    try {
      tableLoading.value = true;
      queryForm.tenantId = props.enterpriseId;
      let res = await enterpriseApi.queryPageEmployeeList(queryForm);
      if(res.success){
        tableData.value = res.data.data;
        total.value = res.data.total;
      }
    } catch (e) {
      smartSentry.captureError(e);
    } finally {
      tableLoading.value = false;
    }
  }

  watch(
    () => props.enterpriseId,
    (e) => {
      if (e) {
        queryEmployee();
      }
    },
    { immediate: true }
  );
</script>

<style scoped lang="less">
  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin: 20px 0;
  }

  .button-style {
    margin: 0 10px;
  }
</style>
